package com.example.supermarket.controller;

import com.example.supermarket.entity.Goods;
import com.example.supermarket.mapper.OrderMapper;
import com.example.supermarket.service.AdminGoodsService;
import com.example.supermarket.service.MemberHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>会员模块</p>
 * @author Bosen
 * @date 2021/10/12 14:14
 */
@RestController
public class MemberHomeController {
    @Autowired
    AdminGoodsService goodsService;

    @Autowired
    MemberHomeService service;

    @Autowired
    OrderMapper orderMapper;

    /**
     * <p>会员主页</p>
     */
    @RequestMapping({"/member/index", "/member"})
    public ModelAndView index(ModelAndView modelAndView) {
        // 获取所有商品信息
        List<Goods> goodsList = goodsService.findAll();
        modelAndView.addObject("goodsList", goodsList);

        modelAndView.setViewName("member/index");
        return modelAndView;
    }

    /**
     * <p>会员登录页面</p>
     */
    @RequestMapping("/member/login")
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("member/login");
        return modelAndView;
    }

    /**
     * <p>退出登录</p>
     */
    @RequestMapping("/member/logout")
    public ModelAndView logout(HttpServletRequest request, ModelAndView modelAndView) {
        modelAndView.setViewName("member/login");
        service.logout(request);
        return modelAndView;
    }

    /**
     * <p>登录处理</p>
     * @return 1 登录成功 ; -1 登录失败
     */
    @RequestMapping("/member/check/login/info")
    public int checkLoginInfo(HttpServletRequest request, String name, String password) {
        return service.checkLoginInfo(request, name, password) ? 1 : -1;
    }

    /**
     * <p>商品购买，下订单</P>
     * @Date 2021/10/13 22:54
     */
    @RequestMapping("/member/buy")
    public int order(HttpServletRequest request, int id){
        int result = service.order(request,id,0);
        return result;


    }

    @RequestMapping("/member/nopay")
    public int noPayOrder(HttpServletRequest request){
        String orderId = (String) request.getSession().getAttribute("orderId");
        int result = orderMapper.updateOrder(orderId,1,System.currentTimeMillis());

        return result;
    }



}
